using System;

using Deepcode.Flux.Core.Services.XML;
using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Systems.Workflow.Info;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow.Attribs;

namespace Deepcode.Flux.Core.Systems.Workflow.BaseHandlers
{
	/// <summary>
	/// BaseHandler is the base workflow handler for general workflow.
	/// It implements common workflow actions such as GotoProcess
	/// </summary>
	public class BaseHandler : Deepcode.Flux.Core.Systems.Workflow.Interfaces.IWorkflowHandler
	{
		#region Definition deletion / edition
		/// <summary>
		/// Implements standard code to see if a definition can be deleted.
		/// </summary>
		/// <param name="DefinitionID"></param>
		/// <returns></returns>
		public virtual bool CanBeDeleted(int DefinitionID)
		{
			if( ! CanBeEdited( DefinitionID )) return false;
			return true;
		}
		/// <summary>
		/// Implements standard code to see if a definition can be edited.
		/// </summary>
		/// <param name="DefinitionID"></param>
		/// <returns></returns>
		public virtual bool CanBeEdited(int DefinitionID)
		{
			Tasks tasks = new Tasks();
			if( tasks.TasksExistForDefinition( DefinitionID )) return false;
			return true;
		}
		/// <summary>
		/// Implement functionality here to clean up any records if a workflow definition
		/// is deleted from the system. The engine will take care of any cleanup in the
		/// workflow definitions tables and tasks list, but any additional tables you might
		/// use during a workflow should be cleared in this function.
		/// </summary>
		/// <param name="DefinitionID"></param>
		public virtual void OnDeleteWorkflowDefinition( int DefinitionID )
		{
		}
		#endregion
		#region Workflow Operator Queries
		/// <summary>
		/// Returns a WorkflowTaskableOperators object representing all
		/// the operators on the system that can have tasks raised against them
		/// </summary>
		/// <returns></returns>
		public virtual WorkflowTaskableOperators GetTaskableOperators()
		{
			WorkflowTaskableOperators ops = new WorkflowTaskableOperators();
			return ops;
		}
		/// <summary>
		/// Returns a WorkflowTaskableGroups object representing all the
		/// operator groups on the system that can have tasks against them
		/// </summary>
		/// <returns></returns>
		public virtual WorkflowTaskableGroups GetTaskableGroups()
		{
			WorkflowTaskableGroups grps = new WorkflowTaskableGroups();
			return grps;
		}
		/// <summary>
		/// Returns a single taskable operator by ID
		/// </summary>
		/// <param name="OperatorID"></param>
		/// <returns></returns>
		public virtual WorkflowTaskableOperator GetTaskableOperator( int OperatorID )
		{
			return null;
		}
		/// <summary>
		/// Returns a single taskable operator group
		/// </summary>
		/// <param name="GroupID"></param>
		/// <returns></returns>
		public virtual WorkflowTaskableGroup GetTaskableGroup( int GroupID )
		{
			return null;
		}
		/// <summary>
		/// Returns all taskable operators within a particular group
		/// </summary>
		/// <param name="GroupID"></param>
		/// <returns></returns>
		public virtual WorkflowTaskableOperators GetTaskableOperatorsForGroup(int GroupID)
		{
			WorkflowTaskableOperators ops = new WorkflowTaskableOperators();
			return ops;
		}
		#endregion
		#region Standard Workflow Actions
		/// <summary>
		/// GotoProcess implements standard branching logic to enable a workflow
		/// task to jump to another workflow process within the same definition.
		/// </summary>
		[WorkflowMethod("Goto another process", "Deepcode.Flux.Core.Systems.Workflow.BaseHandlers.GotoProcessConfig,Deepcode.Flux.Core")]
		public void GotoProcess(WorkflowEngine Engine, int ProcessID, int DataID, string DataName, string propertyBag)
		{
			// Load the property bag
			PropertyBag pb = new PropertyBag( propertyBag );
			// Get the destination process ID
			string sGPID = pb.Get("GotoProcessID", "-1");
			// Convert it to a number
			int GPID = -1;
			try{ GPID = Int32.Parse( sGPID ); } 
			catch{ GPID = -1; }
			if( GPID == -1 ) return;
			// Ask the engine to start processing the destination process
			Engine.InvokeProcess( GPID, DataID, DataName );
		}
		#endregion
		#region URLs for Edit and View
		/// <summary>
		/// Override and return string.empty if the data can never be edited,
		/// or return the URL to edit the data associated with the task object
		/// </summary>
		/// <param name="Task"></param>
		/// <returns></returns>
		public virtual string GetEditURL( Task task )
		{
			return String.Empty;
		}
		/// <summary>
		/// Override and return string.empty if the data can never be viewed,
		/// or return the URL to view the data associated with the task object
		/// </summary>
		/// <param name="Task"></param>
		/// <returns></returns>
		public virtual string GetViewURL( Task task )
		{
			return String.Empty;
		}
		#endregion
		#region Data Dictionary
		/// <summary>
		/// Returns the value of a data dictionary item from the object being managed
		/// </summary>
		/// <param name="Key"></param>
		/// <param name="DataID"></param>
		/// <returns></returns>
		public virtual string DataDictionaryItem( string Key, int DataID )
		{
			return "";
		}
		/// <summary>
		/// Return information concerning what information is exposed from the 
		/// object into the workflow.
		/// </summary>
		/// <returns></returns>
		public virtual WorkflowDataDictionaryDescriptor GetDataDictionary()
		{
			return new WorkflowDataDictionaryDescriptor();
		}
		#endregion
	}
}
